package com.jeesite.modules.sys.service.support;

import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.sys.dao.OrganizationDao;
import com.jeesite.modules.sys.entity.Organization;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 组织账套Service
 * @author 码脑洞
 * @version 2025-04-07
 */
@Service
public class OrganizationService extends CrudService<OrganizationDao, Organization> {
	
	/**
	 * 获取单条数据
	 * @param organization
	 * @return
	 */
	@Override
	public Organization get(Organization organization) {
		return super.get(organization);
	}
	
	/**
	 * 查询分页数据
	 * @param organization 查询条件
	 * @param organization page 分页对象
	 * @return
	 */
	@Override
	public Page<Organization> findPage(Organization organization) {
		return super.findPage(organization);
	}

	/**
	 * 查询列表数据
	 * @return
	 */
	public List<Organization> findList(){
		return this.findList(new Organization());
	}
	
	/**
	 * 查询列表数据
	 * @param organization
	 * @return
	 */
	@Override
	public List<Organization> findList(Organization organization) {
		return super.findList(organization);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param organization
	 */
	@Override
	@Transactional
	public void save(Organization organization) {
		super.save(organization);
	}
	
	/**
	 * 更新状态
	 * @param organization
	 */
	@Override
	@Transactional
	public void updateStatus(Organization organization) {
		super.updateStatus(organization);
	}
	
	/**
	 * 删除数据
	 * @param organization
	 */
	@Override
	@Transactional
	public void delete(Organization organization) {
		super.delete(organization);
	}

	/**
	 * 校验组织编码是否已存在
	 * @param orgCode 组织编码
	 * @param excludeOrgCode 要排除的组织编码(用于更新时排除自身)
	 * @return true表示已存在，false表示不存在
	 */
	public boolean isOrgCodeExist(String orgCode, String excludeOrgCode) {
		Organization where = new Organization();
		where.setOrgCode(orgCode);
		if (StringUtils.isNotBlank(excludeOrgCode)) {
			where.sqlMap().getWhere().and("org_code", QueryType.NE, excludeOrgCode);
		}
		return findCount(where) > 0;
	}

	/**
	 * 根据组织编码查询数据
	 * @param orgCode
	 */
	public Organization findByOrgCode(String orgCode) {
		return dao.findByOrgCode(orgCode);
	}
}